package demo1;

public class MySingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }
    public  ListNode head;
    public void createList(){
        //创建每一个节点
       ListNode node1 = new ListNode(12);
       ListNode node2 = new ListNode(23);
       ListNode node3 = new ListNode(34);
       ListNode node4 = new ListNode(45);

       //把每个节点串起来，形成链表
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
    }
    public void show(){
        ListNode cur = this.head;
     while(cur != null){
         System.out.print(cur.val + " ");
         cur = cur.next;
     }
        System.out.println();
    }
    public void show(ListNode head){
        ListNode cur = head;
        if(head == null) {
            return;
        }
        while(head != null){
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println();

    }
    //头插法

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(cur == null){
            head = node;
            return;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;

    }
    //任意位置插⼊,第⼀个数据节点为0号下标

    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        int num = size();
        if(cur == null){
            return;
        }
        if(index == 0){
            addFirst( data);
            return;
        }
        if(index == num){
            addLast(data);
            return;
        }
        if(index < 0 || index > num){
            return;
        }
        for(int i = index - 1; i > 0;i--){
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字 key是否在单链表当中
    public boolean contains(int key){
      ListNode cur = this.head;
      while(cur != null){
          if(cur.val == key){
              return true;
          }
          cur = cur.next;
      }
        return false;
    }
    //删除第⼀次出现关键字为 key的节点

    public void remove(int key){
      ListNode cur =  this.head;
      if(cur == null){
          return;
      }
      if( cur.val== key ){
       head = head.next;
       return;
     }
      /*while(cur.next != null){
          if(cur.next.val == key){
              cur.next = cur.next.next;
              return;
          }

          cur = cur.next;*/
        cur = findNodePrev(key);
        if(cur != null){
            cur.next = cur.next.next;
      }
        return;
    }
    public ListNode findNodePrev(int key){
        ListNode prev = head;
        while(prev.next !=  null){
            if(prev.next.val == key){
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
    //删除所有值为 key的节点

    public void removeAllKey(int key){
       /* ListNode cur = this.head;
        if(head == null){
            return;
        }
        ListNode prev = cur.next;

       while(prev != null){
           if(prev.val == key){
               prev = prev.next;
               cur.next = prev;
           }else {
               cur.next = prev;
               cur = prev;
               prev = prev.next;
           }
       }
        if( cur.val ==key){
            head =  null;
            return;
        }
        return;*/

            if(head == null) {
                return;
            }
        /*while (head != null && head.val == key) {
            head = head.next;
            //return;
        }
        if(head == null) {
            return;
        }*/
            ListNode prev = this.head;
            ListNode cur = head.next;
            while (cur != null) {
                if(cur.val == key) {
                    prev.next = cur.next;
                    cur = cur.next;
                }else {
                    prev = cur;
                    cur = cur.next;
                }
            }
            if(head.val == key) {
                head = head.next;
            }
        }

    //得到单链表的⻓度

    public int size(){
        if(head == null){
            return -1;
        }
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
           cur = cur.next;
           count++;
        }
        return count;
    }
    public void clear() {
    }
    public void display() {}
    //oj题
    public ListNode removeAllKey(ListNode head,int val){
        if(head == null){
            return head;
        }
        ListNode cur = head;
        ListNode prev = head.next;
        while(prev != null){
            if(prev.val == val){
                cur.next = prev.next;

            }else {
                cur = prev;

            }
            prev = prev.next;
        }
        if(head.val == val){
            head = head.next;
            return head;
        }
       return head;
    }
}
